﻿using System;
using System.Collections.Generic;
using Slippor.Douban.Radio.Assist;
using Slippor.Douban.Radio.Handler;

namespace Slippor.Douban.Radio.BaseForm
{
    public class DoubanRadioHandlerService : IHandlerService
    {
        private readonly Dictionary<String, IHandler> _handlers;

        public DoubanRadioHandlerService()
        {
            _handlers = new Dictionary<String, IHandler>();
        }

        #region IHandlerService Members

        public Dictionary<String, IHandler> Handlers
        {
            get { return _handlers; }
        }

        public void AddHandler(IHandler handler)
        {
            Handlers.Add(handler.Name, handler);
        }

        public void RemoveHandler(IHandler handler)
        {
            if(Handlers.ContainsKey(handler.Name))
            {
                Handlers[handler.Name].Drop(Song.DefaultSong);
            }
            Handlers.Remove(handler.Name);
        }

        public string Name
        {
            get { throw new NotImplementedException(); }
        }

        public void Hand(Song song)
        {
            foreach (IHandler handler in _handlers.Values)
            {
                handler.Hand(song);
            }
        }

        public void Drop(Song song)
        {
            foreach (IHandler handler in _handlers.Values)
            {
                handler.Drop(song);
            }
        }

        #endregion
    }
}